#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 200005
int T,n,ans,l,r,a[N];
bool b[1000005],nx[N];
signed main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    scanf("%d",&T);
    for(int k=1;k<=T;k++){
        scanf("%d",&n); l=0; r=0; ans=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        for(int i=n;i>0;i--){
            nx[i]=b[a[i]];
            b[a[i]]=1;
        }
        for(int i=1;i<=n;i++){
            if(a[l]==a[i]){
                ans+=a[i]; l=i;
                continue;
            }
            else if(a[r]==a[i]){
                ans+=a[i]; r=i;
                continue;
            }
            if(!nx[l]) l=i;
            else if(!nx[r]) r=i;
            else {
                if(a[r]>a[l]) l=i;
                else r=i;
            }
        }
        printf("%d\n",ans);
        for(int i=1;i<=n;i++) nx[i]=0,b[a[i]]=0;
    }
    return 0;
}
